In-class Exercise 4

In this exercise, the focus will be on Onemap SG API and using spatstat

Lye Jia Wei https://lye-jia-wei.github.io/
09-06-2021

1. Onemap SG API

1.1 Getting Started

Installing and importing Onemap SG API.

packages <- c('onemapsgapi')
for(p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p, character.only = T)
}

1.2 Query

Note: Need to put double quote for token and query name

2. In Class Exercise 4

2.1 Getting Started

In this in-class exercise, we will be using the following library:

packages <- c('maptools','sf','raster','spatstat','tmap','tidyverse')
for(p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p, character.only = T)
}

2.1 Getting Started

sg_sf <- st_read(dsn="data/shapefile", layer="CostalOutline")
Reading layer `CostalOutline' from data source 
  `C:\Users\User\Desktop\IS415\lye-jia-wei\IS415_blog\_posts\2021-09-06-in-class-exercise-4\data\shapefile' 
  using driver `ESRI Shapefile'
Simple feature collection with 60 features and 4 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 2663.926 ymin: 16357.98 xmax: 56047.79 ymax: 50244.03
Projected CRS: SVY21
mpsz_sf <- st_read(dsn="data/shapefile",layer="MP14_SUBZONE_WEB_PL")
Reading layer `MP14_SUBZONE_WEB_PL' from data source 
  `C:\Users\User\Desktop\IS415\lye-jia-wei\IS415_blog\_posts\2021-09-06-in-class-exercise-4\data\shapefile' 
  using driver `ESRI Shapefile'
Simple feature collection with 323 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21

2.2 Importing Geospatial Data

sg_sf <- st_read(dsn="data/shapefile", layer="CostalOutline")
Reading layer `CostalOutline' from data source 
  `C:\Users\User\Desktop\IS415\lye-jia-wei\IS415_blog\_posts\2021-09-06-in-class-exercise-4\data\shapefile' 
  using driver `ESRI Shapefile'
Simple feature collection with 60 features and 4 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 2663.926 ymin: 16357.98 xmax: 56047.79 ymax: 50244.03
Projected CRS: SVY21
mpsz_sf <- st_read(dsn="data/shapefile",layer="MP14_SUBZONE_WEB_PL")
Reading layer `MP14_SUBZONE_WEB_PL' from data source 
  `C:\Users\User\Desktop\IS415\lye-jia-wei\IS415_blog\_posts\2021-09-06-in-class-exercise-4\data\shapefile' 
  using driver `ESRI Shapefile'
Simple feature collection with 323 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21

2.3 Importing Aspatial Data

childcare <- read_rds("C:/Users/User/Desktop/IS415/lye-jia-wei/IS415_blog/_posts/2021-09-06-in-class-exercise-4/data/rds/childcare.rds")
CHAS <- read_rds("C:/Users/User/Desktop/IS415/lye-jia-wei/IS415_blog/_posts/2021-09-06-in-class-exercise-4/data/rds/CHAS.rds")

2.4 Converting from aspatial to geospatial

CHAS_sf <- st_as_sf(CHAS, coords = c("X_COORDINATE","Y_COORDINATE"), crs=3414)
childcare$Lat <- as.numeric(childcare$Lat)
childcare$Lng <- as.numeric(childcare$Lng)
childcare_sf <- st_as_sf(childcare, coords= c("Lng","Lat"),crs=4326) %>% st_transform(crs=3414)

3.0 Geospatial Data Wrangling

3.1 Converting from sf to Spatial classes

childcare <- as_Spatial(childcare_sf)
CHAS <- as_Spatial(CHAS_sf)
mpsz <- as_Spatial(mpsz_sf)
sg <- as_Spatial(sg_sf)

3.2 Converting from Spatial classes to SP format

childcare_sp <- as(childcare, "SpatialPoints")
CHAS_sp <- as(CHAS, "SpatialPoints")
sg_sp <- as(sg, "SpatialPolygons")

3.3 Converting from sp to spatstat ppp format

childcare_ppp <- as(childcare_sp, "ppp")
CHAS_ppp<- as(CHAS_sp, "ppp")

3.4 Interative Map

tmap_mode("view")
tm_shape(childcare_sf) + 
  tm_dots(alpha=0.4, col= "blue", size=0.05) +
  tm_shape(CHAS_sf) +
  tm_dots(alpha=0.4, col="red", size=0.05)